package com.yanqu.road.server.cmd.activity.yingxin;

import com.yanqu.road.pb.activity.YingXinProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.yingxin.YingXinModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_ACTIVITY_YINGXIN_SHOOT_BALL, desc = "迎新夜宴发射球")
public class YingXinShootBallCmd extends AbstractCommand {

    private int angle;
    private int height;
    private int multi;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        YingXinProto.YingXinShootBallReqMsg reqMsg = YingXinProto.YingXinShootBallReqMsg.parseFrom(packet.getBytes());
        angle = reqMsg.getAngle();
        height = reqMsg.getHeight();
        multi = reqMsg.getMultiType();

        // 角度不能超出范围，否则自动调整
        if (angle < -75) {
            angle = -75;
        }
        if (angle > 75) {
            angle = 75;
        }
        // 高度也有限制
        if (height < 800) {
            height = 800;
        }
        if (height > 1800) {
            height = 1800;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        player.getModule(YingXinModule.class).checkUserData();
        ret = player.getModule(YingXinModule.class).shootBall(angle, height, multi);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            YingXinProto.YingXinShootBallRespMsg.Builder builder = YingXinProto.YingXinShootBallRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_ACTIVITY_YINGXIN_SHOOT_BALL, builder);
        }
    }
}
